﻿#region zh-CHS 2006 - 2010 DemoSoft 团队 | en 2006-2010 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the MMOSE(Massively Multiplayer Online Server Engine) for .NET.
//
//                              2006-2010 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Diagnostics;
using System.Threading;
using Demo.Mmose.Core.Common;
using Demo.Mmose.Core.Common.SafeCollections;
using Demo.Mmose.Core.Util;
#endregion

namespace Demo.Mmose.Core.Entity.Suit.Mail
{
    /// <summary>
    /// 
    /// </summary>
    public class MailBoxManager<MailBoxT, MailMessageT>
        where MailBoxT : MailBox<MailMessageT>
        where MailMessageT : MailMessage
    {
        #region zh-CHS 共有属性 | en Public Properties

        #region zh-CHS MailBox列表 属性 | en Public Properties
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private SafeDictionary<long, MailBoxT> m_MailBoxs = new SafeDictionary<long, MailBoxT>();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        [MultiThreadedWarning( "zh-CHS", "(邮件信息)当前的数组是列表临时产生的,不能保存数组用于以后操作:警告!" )]
        public MailBoxT[] ToArray()
        {
            return m_MailBoxs.ToArrayValues();
        }

        #region zh-CHS 共有方法 | en Public Methods

        /// <summary>
        /// 添加信箱
        /// </summary>
        /// <param name="item"></param>
        public bool AddMailBox( long iMailBoxSerial, MailBoxT mailBoxT )
        {
            if ( mailBoxT == null )
                throw new ArgumentNullException( "mailBoxT", "MailBoxManager<MailBoxT, MailMessageT>.AddMailBox(...) - mailBoxT == null error!" );

            EventHandler<AddingMailBoxCallEventArgs<MailBoxT, MailMessageT>> tempBeforeEventArgs = m_EventAddingMailBox;
            if ( tempBeforeEventArgs != null )
            {
                AddingMailBoxCallEventArgs<MailBoxT, MailMessageT> eventArgs = new AddingMailBoxCallEventArgs<MailBoxT, MailMessageT>( mailBoxT, this );
                tempBeforeEventArgs( this, eventArgs );

                if ( eventArgs.IsCancel == true )
                    return false;
            }

            m_MailBoxs.Add( iMailBoxSerial, mailBoxT );

            EventHandler<AddedMailBoxCallEventArgs<MailBoxT, MailMessageT>> tempAfterEventArgs = m_EventAddedMailBox;
            if ( tempAfterEventArgs != null )
            {
                AddedMailBoxCallEventArgs<MailBoxT, MailMessageT> eventArgs = new AddedMailBoxCallEventArgs<MailBoxT, MailMessageT>( mailBoxT, this );
                tempAfterEventArgs( this, eventArgs );
            }

            return true;
        }

        /// <summary>
        /// 移出信箱
        /// </summary>
        /// <param name="item"></param>
        public bool RemoveMailBox( long iMailBoxSerial )
        {
            MailBoxT mailBoxT = FindMailBoxOnSerial( iMailBoxSerial );
            if ( mailBoxT == null )
                return false;

            EventHandler<RemovingMailBoxCallEventArgs<MailBoxT, MailMessageT>> tempBeforeEventArgs = m_EventRemovingMailBox;
            if ( tempBeforeEventArgs != null )
            {
                RemovingMailBoxCallEventArgs<MailBoxT, MailMessageT> eventArgs = new RemovingMailBoxCallEventArgs<MailBoxT, MailMessageT>( mailBoxT, this );
                tempBeforeEventArgs( this, eventArgs );

                if ( eventArgs.IsCancel == true )
                    return false;
            }

            m_MailBoxs.Remove( iMailBoxSerial );

            EventHandler<RemovedMailBoxCallEventArgs<MailBoxT, MailMessageT>> tempAfterEventArgs = m_EventRemovedMailBox;
            if ( tempAfterEventArgs != null )
            {
                RemovedMailBoxCallEventArgs<MailBoxT, MailMessageT> eventArgs = new RemovedMailBoxCallEventArgs<MailBoxT, MailMessageT>( mailBoxT, this );
                tempAfterEventArgs( this, eventArgs );
            }

            return true;
        }

        /// <summary>
        /// 在信箱集找信箱
        /// </summary>
        /// <param name="layer"></param>
        /// <returns></returns>
        public MailBoxT FindMailBoxOnSerial( long iMailBoxSerial )
        {
            return m_MailBoxs.GetValue( iMailBoxSerial );
        }


        /// <summary>
        /// 发送信箱
        /// </summary>
        /// <param name="layer"></param>
        /// <returns></returns>
        public void PostMailMessage( long iMailBoxSerial, MailMessageT mailMessage )
        {
            MailBoxT mailBoxT = FindMailBoxOnSerial( iMailBoxSerial );
            if ( mailBoxT == null )
                return;

            // 添加信箱
            mailBoxT.AddMailMessage( mailMessage.Serial, mailMessage );
        }
        #endregion

        #endregion

        #endregion

        #region zh-CHS 共有事件 | en Public Event

        #region zh-CHS AddMailBoxCall事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<AddingMailBoxCallEventArgs<MailBoxT, MailMessageT>> m_EventAddingMailBox;
        /// <summary>
        /// 
        /// </summary>
        private SpinLock m_LockEventAddingMailBox = new SpinLock();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<AddingMailBoxCallEventArgs<MailBoxT, MailMessageT>> AddingMailBox
        {
            add
            {
                SpinLockEx.ReliableEnter( ref m_LockEventAddingMailBox );
                try
                {
                    m_EventAddingMailBox += value;
                }
                finally
                {
                    m_LockEventAddingMailBox.Exit();
                }
            }
            remove
            {
                SpinLockEx.ReliableEnter( ref m_LockEventAddingMailBox );
                try
                {
                    m_EventAddingMailBox -= value;
                }
                finally
                {
                    m_LockEventAddingMailBox.Exit();
                }
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<AddedMailBoxCallEventArgs<MailBoxT, MailMessageT>> m_EventAddedMailBox;
        /// <summary>
        /// 
        /// </summary>
        private SpinLock m_LockEventAddedMailBox = new SpinLock();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<AddedMailBoxCallEventArgs<MailBoxT, MailMessageT>> AddedMailBox
        {
            add
            {
                SpinLockEx.ReliableEnter( ref m_LockEventAddedMailBox );
                try
                {
                    m_EventAddedMailBox += value;
                }
                finally
                {
                    m_LockEventAddedMailBox.Exit();
                }
            }
            remove
            {
                SpinLockEx.ReliableEnter( ref m_LockEventAddedMailBox );
                try
                {
                    m_EventAddedMailBox -= value;
                }
                finally
                {
                    m_LockEventAddedMailBox.Exit();
                }
            }
        }
        #endregion

        #region zh-CHS RemoveMailBoxCall事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<RemovingMailBoxCallEventArgs<MailBoxT, MailMessageT>> m_EventRemovingMailBox;
        /// <summary>
        /// 
        /// </summary>
        private SpinLock m_LockEventRemovingMailBox = new SpinLock();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<RemovingMailBoxCallEventArgs<MailBoxT, MailMessageT>> RemovingMailBox
        {
            add
            {
                SpinLockEx.ReliableEnter( ref m_LockEventRemovingMailBox );
                try
                {
                    m_EventRemovingMailBox += value;
                }
                finally
                {
                    m_LockEventRemovingMailBox.Exit();
                }
            }
            remove
            {
                SpinLockEx.ReliableEnter( ref m_LockEventRemovingMailBox );
                try
                {
                    m_EventRemovingMailBox -= value;
                }
                finally
                {
                    m_LockEventRemovingMailBox.Exit();
                }
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<RemovedMailBoxCallEventArgs<MailBoxT, MailMessageT>> m_EventRemovedMailBox;
        /// <summary>
        /// 
        /// </summary>
        private SpinLock m_LockEventRemovedMailBox = new SpinLock();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<RemovedMailBoxCallEventArgs<MailBoxT, MailMessageT>> RemovedMailBox
        {
            add
            {
                SpinLockEx.ReliableEnter( ref m_LockEventRemovedMailBox );
                try
                {
                    m_EventRemovedMailBox += value;
                }
                finally
                {
                    m_LockEventRemovedMailBox.Exit();
                }
            }
            remove
            {
                SpinLockEx.ReliableEnter( ref m_LockEventRemovedMailBox );
                try
                {
                    m_EventRemovedMailBox -= value;
                }
                finally
                {
                    m_LockEventRemovedMailBox.Exit();
                }
            }
        }
        #endregion

        #endregion
    }

    /// <summary>
    /// 
    /// </summary>
    public class MailBoxManager : MailBoxManager<MailBox<MailMessage>, MailMessage>
    {

    }
}
#endregion